// src/types/form.d.ts
import { ServiceVersionVOType } from '@coolphk/shared-types/low-code'

/**
 * 获取默认表单配置
 */
export type FormConfig = {
  modelName?: string // 表单数据模型名称，默认值为 'formData'
  refName?: string // 表单引用名称，默认值为 'vForm'
  rulesName?: string // 验证规则名称，默认值为 'rules'
  labelWidth?: number // 标签宽度，默认值为 80
  labelPosition?: string // 标签位置，默认值为 'left'
  modelSelect?: string // 模型选择，默认值为空字符串
  modelSelectTree?: any[] // 模型选择树，默认值为空数组
  modelFormTree?: any[] // 模型表单树，默认值为空数组
  labelAlign?: string // 标签对齐方式，默认值为 'label-left-align'
  cssCode?: string // 自定义CSS代码，默认值为空字符串
  customClass?: string[] // 自定义类，默认值为空数组
  functions?: string // 全局函数，默认值为空字符串
  layoutType?: string // 布局类型，默认值为 'PC'
  jsonVersion?: number // JSON版本，默认值为 3
  dataSources?: any[] // 数据源集合，默认值为空数组
  serviceVersionVOs?: ServiceVersionVOType[] // 表单与服务版本的绑定关系，默认值为空数组
  service?: {
    enabled: boolean // 是否启用服务，默认值为 false
    key: string // 服务键，默认值为空字符串
  } // 服务配置
  onFormCreated?: string // 表单创建时的回调函数，默认值为空字符串
  onFormMounted?: string // 表单挂载时的回调函数，默认值为空字符串
  onFormDataChange?: string // 表单数据变化时的回调函数，默认值为空字符串
  onFormValidate?: string // 表单验证时的回调函数，默认值为空字符串
}

export type DesignerConfig = {
  languageMenu: boolean,  //是否显示语言切换菜单
  externalLink: boolean,  //是否显示GitHub、文档等外部链接
  formTemplates: boolean,  //是否显示表单模板
  componentLib: boolean,  //是否显示组件库
  chartLib: boolean,  //是否显示图表库
  metadataLib: boolean,  //是否显示元数据
  layoutTypeButton: boolean,  //是否显示表单布局适配按钮组
  eventCollapse: boolean,  //是否显示组件事件属性折叠面板
  widgetNameReadonly: boolean,  //禁止修改组件名称

  clearDesignerButton: boolean,  //是否显示清空设计器按钮
  previewFormButton: boolean,  //是否显示预览表单按钮
  importJsonButton: boolean,  //是否显示导入JSON按钮
  exportJsonButton: boolean,  //是否显示导出JSON器按钮
  exportCodeButton: boolean,  //是否显示导出代码按钮
  generateSFCButton: boolean,  //是否显示生成SFC按钮
  logoHeader: boolean,  //是否显示Logo头部区域（仅Pro）
  saveButton: boolean,  //是否显示保存按钮

  toolbarMaxWidth: number,  //设计器工具按钮栏最大宽度（单位像素）
  toolbarMinWidth: number,  //设计器工具按钮栏最小宽度（单位像素）

  productName: string,  //自定义表单设计器名称，对应“VForm Pro”（仅Pro）
  productTitle: string,  //自定义表单设计器标题，对应“表单设计器”（仅Pro）

  presetCssCode: string,  //设计器预设CSS样式代码
  languageName: string,  //界面语言，默认显示中文

  resetFormJson: boolean,  //是否在设计器初始化时将表单内容重置为空
}

export interface FormDesigner {
  widgetList: any[];
  formConfig: FormConfig;

  selectedId: string | null;
  selectedWidget: any | null;
  selectedWidgetName: string | null;
  vueInstance: any;

  formWidget: any | null;
  cssClassList: string[];

  historyData: {
    index: number;
    maxStep: number;
    steps: any[];
  };

  currentServiceVersionVOs: ServiceVersionVOType[];   //当前表单相关的发布服务版本集合

  initDesigner(resetFormJson: boolean): void;

  compareServiceVersions(): void

  clearDesigner(skipHistoryChange: boolean): void;

  loadPresetCssCode(preCssCode: string): void;

  getLayoutType(): string;

  changeLayoutType(newType: string): void;

  getImportTemplate(): { widgetList: any[], formConfig: FormConfig };

  loadFormJson(formJson: any): boolean;

  setSelected(selected: any): void;

  updateSelectedWidgetNameAndLabel(selectedWidget: any, newName: string, newLabel: string): void;

  clearSelected(): void;

  checkWidgetMove(evt: any): boolean;

  checkFieldMove(evt: any): boolean;

  appendTableRow(widget: any): void;

  appendTableCol(widget: any): void;

  insertTableRow(widget: any, insertPos: number, cloneRowIdx: number, curCol: number, aboveFlag: boolean): void;

  insertTableCol(widget: any, insertPos: number, curRow: number, leftFlag: boolean): void;

  setPropsOfMergedCols(rowArray: any[], startRowIndex: number, startColIndex: number, newColspan: number, rowspan: number): void;

  setPropsOfMergedRows(rowArray: any[], startRowIndex: number, startColIndex: number, colspan: number, newRowspan: number): void;

  setPropsOfSplitCol(rowArray: any[], startRowIndex: number, startColIndex: number, colspan: number, rowspan: number): void;

  setPropsOfSplitRow(rowArray: any[], startRowIndex: number, startColIndex: number, colspan: number, rowspan: number): void;

  mergeTableCol(rowArray: any[], colArray: any[], curRow: number, curCol: number, leftFlag: boolean, cellWidget: any): void;

  mergeTableWholeRow(rowArray: any[], colArray: any[], rowIndex: number, colIndex: number): void;

  mergeTableRow(rowArray: any[], curRow: number, curCol: number, aboveFlag: boolean, cellWidget: any): void;

  mergeTableWholeCol(rowArray: any[], colArray: any[], rowIndex: number, colIndex: number): void;

  undoMergeTableCol(rowArray: any[], rowIndex: number, colIndex: number, colspan: number, rowspan: number): void;

  undoMergeTableRow(rowArray: any[], rowIndex: number, colIndex: number, colspan: number, rowspan: number): void;

  deleteTableWholeCol(rowArray: any[], colIndex: number): void;

  deleteTableWholeRow(rowArray: any[], rowIndex: number): void;

  getContainerByType(typeName: string): any;

  getFieldWidgetByType(typeName: string): any;

  hasConfig(widget: any, configName: string): boolean;

  upgradeWidgetConfig(oldWidget: any): void;

  upgradeFormConfig(oldFormConfig: FormConfig): void;

  cloneGridCol(widget: any, parentWidget: any): void;

  cloneContainer(containWidget: any): any;

  cloneFieldWidget(fieldWidget: any, parentList: any[]): void;

  moveUpWidget(parentList: any[], indexOfParentList: number): void;

  moveDownWidget(parentList: any[], indexOfParentList: number): void;

  copyNewFieldWidget(origin: any): any;

  buildColsOfGrid(gridCon: any, colNum: number, colSpan?: number): void;

  copyNewContainerWidget(origin: any): any;

  addContainerByDbClick(container: any): void;

  addFieldByDbClick(widget: any): void;

  findDashboardContainer(): any;

  addChartContainerByDbClick(container: any): void;

  addChartByDbClick(chart: any): void;

  deleteColOfGrid(gridWidget: any, colIdx: number): void;

  insertNewColOfGrid(gridWidget: any, colIdx: number): void;

  addNewColOfGrid(gridWidget: any): void;

  addTabPaneOfTabs(tabsWidget: any): void;

  deleteTabPaneOfTabs(tabsWidget: any, tpIdx: number): void;

  emitEvent(evtName: string, evtData: any[]): void;

  handleEvent(evtName: string, callback: (data: any) => void): void;

  setCssClassList(cssClassList: string[]): void;

  getCssClassList(): string[];

  registerFormWidget(formWidget: any): void;

  initHistoryData(): void;

  emitHistoryChange(): void;

  saveCurrentHistoryStep(): void;

  undoHistoryStep(): void;

  redoHistoryStep(): void;

  undoEnabled(): boolean;

  redoEnabled(): boolean;

  saveFormContentToStorage(): void;

  loadFormContentFromStorage(): void;

  loadCurrentServiceVersionVOS(): void;
}
